<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://static.web.sdo.com/chuanshi/common/18.2.0/react.development.js"></script>
    <script src="https://static.web.sdo.com/chuanshi/common/18.2.0/react-dom.development.js"></script>
    <script src="https://static.web.sdo.com/chuanshi/common/18.2.0/babel.min.js"></script>
    <style>
        ::-webkit-scrollbar {
            position: absolute;
            right: 0;
            top: 0
        }

        ::-webkit-scrollbar {
            width: 8px;
            height: 8px
        }

        ::-webkit-scrollbar-thumb {
            background: #ec6756;
            -webkit-border-radius: 5px;
            border-radius: 5px
        }

        ::-webkit-scrollbar-track {
            background: 0 0
        }

        .box-wrapper {
            max-height: 200px;
            overflow: scroll;
        }
    </style>
</head>
<body>
    <div id="app"></div>
</body>
</html>
<script type="text/babel">
    class App extends React.Component {
        state = { list: [] }
        componentDidMount() {
            setInterval(() => {
                const { list } = this.state;
                const item = `第${list.length + 1}条新闻`;
                this.setState({
                    list: [item, ...list]
                })
            }, 1000)
        }
        getSnapshotBeforeUpdate() {
            return this.ulRef.scrollHeight;
        }
        componentDidUpdate(prevProps, prevState, snapValue) {
            console.log(snapValue)
            this.ulRef.scrollTop += this.ulRef.scrollHeight - snapValue;
        }
        render() {
            return (
                <div className="container">
                    <div className="mt-4 alert alert-danger">当前共有：{this.state.list.length}条数据</div>
                    <div className="mb-4 box-wrapper">
                        <ul ref={c => this.ulRef = c} className="list-group">
                            {this.state.list.map(item => <li className="list-group-item" key={item}>{item}</li>)}
                        </ul>
                    </div>
                    <button onClick={this.increment} className="btn btn-outline-danger">增加</button>
                </div>
            )
        }
    }
    ReactDOM.createRoot(document.getElementById("app")).render(<App name="app" />)
</script>